﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;


namespace 共享单车管理系统.oeder
{
    public partial class chek_oeder : Form
    {
        public chek_oeder()
        {
            InitializeComponent();
        }
        SqlDataAdapter daorder;
        DataSet ds=new DataSet();
        void init()
        {
            DB.GetCn();
            string str = "select * from orders";
            daorder = new SqlDataAdapter(str, DB.cn);
            daorder.Fill(ds, "order_info");
        }
        void showall()
        {
            init();
            showXz();
        }
        void showXz()
        {
            DataGridViewCheckBoxColumn acCode = new DataGridViewCheckBoxColumn();
            acCode.Name = "acCode"; // 设置列的名称为 "acCode"
            acCode.HeaderText = "选择";
            dvgorder.Columns.Add(acCode);
        }


        private void chek_oeder_Load(object sender, EventArgs e)
        {
            if(Login.Dflag==false)
            {
                MessageBox.Show("需要客服部员工身份登录");
                this.Close();
            }
            else
            {
                showall();
                DataView dvorder = new DataView(ds.Tables["order_info"]);
                dvgorder.DataSource = dvorder;

            }
        }

        private void dvgorder_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if(dvgorder.Rows.Count >0)
            {
                for(int i = 0; i < dvgorder.Rows.Count; i++)
                {
                    DataGridViewCheckBoxCell ck = dvgorder.Rows[i].Cells["acCode"]as DataGridViewCheckBoxCell;
                    if(i!=e.RowIndex)
                    {
                        ck.Value = false;

                    }
                    else
                    {
                        ck.Value = true;
                    }
                }
            }
        }

        private void buttonok_Click(object sender, EventArgs e)
        {
            int s=dvgorder.Rows.Count;
            for(int i = 0;i<dvgorder.Rows.Count;i++)
            {
                if (dvgorder.Rows[i].Cells["acCode"].EditedFormattedValue.ToString()=="True")
                {
                    dvgorder.Rows[i].Cells["status"].Value = "已审核";
                    if (string.IsNullOrEmpty(dvgorder.Rows[i].Cells["EmployeeID"].Value?.ToString()) )
                    {
                        dvgorder.Rows[i].Cells["EmployeeID"].Value = Login.EmployeeID;
                    }
                    UpdateDB();
                }
                else
                {
                    s=s-1;
                }

            }
            if(s==0)
            {
                MessageBox.Show("请选择要审核的项");
            }
        }
        void UpdateDB()
        {
            try
            {
                SqlCommandBuilder dbOrder = new SqlCommandBuilder(daorder);
                daorder.Update(ds, "Order_info");

            }
            catch (SqlException ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
        }

        private void buttoncance_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
